/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager;

import android.app.Activity;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import org.slideme.android.manager.respoitory.UserInfoRepository;
import org.slideme.android.manager.user.UserInfo;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Activity that displays the terms and conditions of the application.
 */
public class TermsAndConditionsActivity extends Activity {

    public static String TERMS;

    public static String CONTENT_URI;

    public static String BUNDLE_ID;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.setContentView(R.layout.tnc_layout);
        TextView terms = (TextView) findViewById(R.id.tnc_terms);
        if (terms == null) {
            showAlert("View error", 0, "Unable to locate the terms view:", "Go back", false);
        }
        if (TERMS != null) {
            terms.setText(TERMS);
        } else {
            terms.setText("No terms specified");
        }

        ImageButton download = (ImageButton) findViewById(R.id.tnc_layout_download);
        download.setOnClickListener(new ImageButton.OnClickListener() {
            public void onClick(View view) {
                if (view == null) {
                    throw new IllegalArgumentException("view");
                }
                SamApplication samApplication = (SamApplication) getApplication();
                ApplicationManagerContext ctx = samApplication.getApplicationManagerContext();
                URI uri;
                try {
                    uri = new URI(CONTENT_URI);
                } catch (URISyntaxException e) {
                    Log.i("TermsAndConditionsActivity", "Invalid URI for Media Descriptor: URI = " + CONTENT_URI, e);
                    return;//display message
                }

                try {
                    ctx.getDownloadContext().getDownloadUserAgent().downloadContent(uri);
                } catch (IOException e) {
                    Log.i("TermsAndConditionsActivity", "Failed to download Media Descriptor: URI = " + CONTENT_URI, e);
                    return;
                }

                //Store in my app list
                UserInfoRepository userInfoRepository = ctx.getUserInfoRepository();
                UserInfo userInfo = userInfoRepository.getUserInfo();
                userInfo.getInstalledBundleIds().add(BUNDLE_ID);
                try {
                    userInfoRepository.storeOrUpdate(userInfo);
                    ContentValues contentValues = new ContentValues();//TODO: move to repo
                    contentValues.put("isInstalled", "true");
                    ctx.getContentBundleRepository().getContentBundleContentProvider()
                            .update(Uri.parse("content://org.slideme/content_bundle/" + BUNDLE_ID), contentValues, null, null);
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });

        ImageButton cancel = (ImageButton) findViewById(R.id.tnc_layout_cancel);
        cancel.setOnClickListener(new ImageButton.OnClickListener() {
            public void onClick(View view) {
                if (view == null) {
                    throw new IllegalArgumentException("view");
                }
                TermsAndConditionsActivity.this.finish();
            }
        });

        ImageButton myAppsView = (ImageButton) this.findViewById(R.id.tnc_layout_myapps_button);
        ImageButton settingsView = (ImageButton) this.findViewById(R.id.tnc_layout_settings_button);
        ImageButton applicationsView = (ImageButton) this.findViewById(R.id.tnc_layout_applications_button);
        ViewContext.setMenuViewListeners(myAppsView, settingsView, applicationsView, this);
    }

}
